package com.sunther.document.controller;

import com.sunther.document.entity.ProjectEntity;
import com.sunther.document.service.ProjectService;
import com.sunther.idb.common.util.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/16 10:03
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/list")
    public WebResult list(@RequestParam(required = false) String keyword) {
        try {
            return WebResult.success("操作成功",projectService.listByKeyword(keyword));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/page_list")
    public WebResult pageList(@RequestParam(required = false) String keyword,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                              @RequestParam(defaultValue = "1") int pageNumber,
                              @RequestParam(defaultValue = "10") int pageSize,
                              HttpServletRequest request) {
        try {
            return WebResult.success(projectService.pageList(keyword,startDate,endDate,pageNumber,pageSize,request));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @PostMapping("/save")
    public WebResult save(@RequestBody ProjectEntity projectEntity,
                          HttpServletRequest request){
        try {
            return projectService.saveOrUpdateProject(projectEntity,request);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String keyword,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        projectService.export(keyword,startDate,endDate,request,response);
    }

    @GetMapping("/delete")
    public WebResult delete(@RequestParam String id) {
        try {
            return projectService.delete(id);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/dtl")
    public WebResult dtl(@RequestParam String id) {
        try {
            return WebResult.success(projectService.dtl(id));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }
}
